package controllers

import (
	"IoTHubAddressService/iotserver/models"
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego"
)

type ServerController struct {
	beego.Controller
}

// @Title GetAll
// @Description get all Servers
// @Success 200 {object} models.ServerInfo
// @Failure 204 no content
// @router / [get]
func (s *ServerController) ServerAll() {
	token:=s.Ctx.Input.Header("Authorization")
	err:=models.CheckToken(token)
	if err!=nil{
		ms:=models.ResponseErrStruct{
			SuccessCode:0,
			MessageCode:2004,
			Message:err.Error(),
		}
		s.Data["json"]=&ms
	}else {
		se,_:=models.ServerAll()
		if len(se)==0{
			ms:=models.ResponseErrStruct{
				SuccessCode:0,
				MessageCode:2004,
				Message:"no content",
			}
			s.Data["json"]=&ms
		}else {
			s.Ctx.ResponseWriter.WriteHeader(200)
			s.Data["json"]=se
		}
	}
	s.ServeJSON()
}

// @Title ServerAdd
// @Description add a server
// @Param	body		body 	models.ServerInfo	true		"body for server content"
// @Success 201 {object} models.ResponseStruct
// @Failure 204 error
// @router / [post]
func (s *ServerController)ServerAdd()  {
	token:=s.Ctx.Input.Header("Authorization")
	err:=models.CheckToken(token)
	if err!=nil{
		ms:=models.ResponseErrStruct{
			SuccessCode:0,
			MessageCode:2004,
			Message:err.Error(),
		}
		s.Data["json"]=&ms
	}else {
		var server models.ServerInfo
		json.Unmarshal(s.Ctx.Input.RequestBody, &server)
		_, err := models.ServerAdd(server)
		if err != nil {
			ms := models.ResponseErrStruct{
				SuccessCode: 0,
				MessageCode: 2004,
				Message:     err.Error(),
			}
			//s.Ctx.ResponseWriter.WriteHeader(204)
			s.Data["json"] = &ms
		} else {
			ms := models.ResponseStruct{
				SuccessCode: 1,
				MessageCode: 2001,
				Message:     server,
			}
			s.Ctx.ResponseWriter.WriteHeader(201)
			s.Data["json"] = &ms
		}
	}
	s.ServeJSON()
}

// @Title GetByName
// @Description get  Servers by name
// @Param	name		path 	string	true		"The key of servername"
// @Success 200 {object} models.ServerInfo
// @Failure 204 no content
// @router /:name [get]
func (s *ServerController)ServerByName()  {
	token:=s.Ctx.Input.Header("Authorization")
	err:=models.CheckToken(token)
	if err!=nil{
		ms:=models.ResponseErrStruct{
			SuccessCode:0,
			MessageCode:2004,
			Message:err.Error(),
		}
		s.Data["json"]=&ms
	}else {
		ser := s.GetString(":name")
		if ser != "" {
			se, _ := models.ServerSearchByName(ser)
			if len(se) == 0 {
				ms := models.ResponseErrStruct{
					SuccessCode: 0,
					MessageCode: 2004,
					Message:     "no content",
				}
				s.Data["json"] = &ms
			} else {
				s.Ctx.ResponseWriter.WriteHeader(200)
				s.Data["json"] = se
			}
		}
	}
	s.ServeJSON()
}
// @Title Delete
// @Description delete the server
// @Param	name		path 	string	true		"The server you want to delete"
// @Success 204 {object} models.ResponseStruct
// @Failure 400 the server don't exists
// @router /:name [delete]
func (s *ServerController)ServerDel()  {
	token:=s.Ctx.Input.Header("Authorization")
	err:=models.CheckToken(token)
	if err!=nil{
		ms:=models.ResponseErrStruct{
			SuccessCode:0,
			MessageCode:2004,
			Message:err.Error(),
		}
		s.Data["json"]=&ms
	}else {
		ser := s.GetString(":name")
		se, _ := models.SearchByName(ser)
		_, err := models.ServerDel(ser)
		if err != nil {
			ms := models.ResponseErrStruct{
				SuccessCode: 0,
				MessageCode: 2004,
				Message:     err.Error(),
			}
			s.Data["json"] = &ms
		} else {
			ms := models.ResponseStruct{
				SuccessCode: 1,
				MessageCode: 2001,
				Message:     se,
			}
			fmt.Println(se)
			s.Ctx.ResponseWriter.Status = 204
			s.Data["json"] = &ms
		}
	}
	s.ServeJSON()
}
